package com.redxun.core.api.impl;

import cn.hutool.core.util.ObjectUtil;
import com.redxun.core.api.config.ICoreConfigService;
import com.redxun.core.boot.constant.CacheConstant;
import com.redxun.core.db.datasource.DataSourceContextHolder;
import com.redxun.core.tool.IoUtil;
import com.redxun.core.tool.StringUtils;
import com.redxun.system.core.service.SysConfigInfoServiceImpl;
import java.io.File;
import java.util.Properties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.stereotype.Component;

/**
 * 配置接口.
 * @author aj
 */
@Component
@Slf4j
public class BootCoreApiConfigImpl implements ICoreConfigService {
    
    @Autowired
    SysConfigInfoServiceImpl configInfoService;

    @Override
    public Properties getConfigProperties(String dataId, String group) {
        Properties properties = configInfoService.getProperties(dataId, group);
        return properties;
    }

    @Override
    public Properties getConfig(String dataId, String group) {
        String content = configInfoService.getConfig(dataId, group);
        Properties properties = new Properties();
        properties.put("", content);
        return properties;
    }

    @Override
    public String getConfig(String dataId, String group, long timeoutMs) {
        return configInfoService.getConfig(dataId, group);
    }

    @Override
    public Properties getStartupConfigProperties(String dataId, String group) {
        Resource resource = new ClassPathResource("/" + dataId);
        Properties properties = new Properties();
        try {
            if (ObjectUtil.isEmpty(resource) || !resource.exists()) {
                String path = System.getProperty("configPath") + File.separator + dataId;
                resource = new FileSystemResource(path);
            }
            properties = PropertiesLoaderUtils.loadProperties(resource);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("read config file " + dataId + " is error!" + "; resource: " + resource.getFilename());
        }
        return properties;
    }

    @Override
    public String getStartupConfig(String dataId, String group) {
        Resource resource = new ClassPathResource("/" + dataId);

        try {
            if (ObjectUtil.isEmpty(resource) || !resource.exists()) {
                String path = System.getProperty("configPath") + File.separator + dataId;
                resource = new FileSystemResource(path);
            }
            return IoUtil.readToString(resource.getInputStream()) ;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("read config file " + dataId + " is error!" + "; resource: " + resource.getFilename());
        }
        return "";
    }


    @Deprecated
    @Override
    public String getConfigAndSignListener(String dataId, String group, long timeoutMs, Object listener) {
        return null;
    }



    @Override
    public boolean publishConfig(String dataId, String group, String content) {
        configInfoService.updateConfig(dataId, group, content);
        return  true;
    }

    @Deprecated
    @Override
    public boolean publishConfig(String alias, String dataId, String group, String content) {
        return false;
    }

    @Deprecated
    @Override
    public boolean removeConfig(String dataId, String group) {
        return false;
    }

    @Deprecated
    @Override
    public void removeListener(String dataId, String group, Object listener) {

    }

    @Deprecated
    @Override
    public String getServerStatus() {
        return null;
    }

    @Deprecated
    @Override
    public void shutDown() {

    }

    @Override
    public String getVal(String key) {
        String ds = DataSourceContextHolder.getDataSource();
        if (StringUtils.isNotEmpty(ds)) {
            DataSourceContextHolder.setDefaultDataSource();
        }
        Properties properties = configInfoService.getProperties(CacheConstant.JPAAS_CONFIG, CacheConstant.DEFAULT_GROUP);
        if (StringUtils.isNotEmpty(ds)) {
            DataSourceContextHolder.setDataSource(ds);
        }
        String str = properties.getProperty("props." + key);
        return str;
    }

    @Override
    public Integer getInt(String key) {
        Integer value = 0;
        try {
            value = Integer.valueOf(getVal(key));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }

    @Override
    public Boolean getBoolean(String key) {
        Boolean value = false;
        try {
            String str = getVal(key);
            if (str == null) {
                str = "false";
            }
            value = Boolean.valueOf(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }

    @Override
    public Long getLong(String key) {
        Long value = 0L;
        try {
            value = Long.valueOf(getVal(key));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }

    @Override
    public String getString(String key) {
        String value = "";
        try {
            value = getVal(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }

    @Deprecated
    @Override
    public void setEnvironment(Environment environment) {

    }

}
